import 'package:farmer_app/common/controller/greenhouse_curtain_controller.dart';
import 'package:farmer_app/common/controller/water_pump_controller.dart';
import 'package:flutter/material.dart';

import '../../../common/app_theme.dart';
import '../../../common/controller/greenhouse_wind_controller.dart';
import '../../../common/tempt/glass_view.dart';
import '../../../common/tempt/running_view.dart';
import '../../../common/tempt/water_view.dart';


class WorkEquipmentComponent extends StatefulWidget {

  final int workSpaceId;
  final ScrollController scrollController;
  final AnimationController? animationController;

  const WorkEquipmentComponent({Key? key, required this.workSpaceId, required this.scrollController, this.animationController}) : super(key: key);

  @override
  State<WorkEquipmentComponent> createState() => _WorkEquipmentComponentState();
}

class _WorkEquipmentComponentState extends State<WorkEquipmentComponent> {
  List<Widget> listViews = <Widget>[];
  late final ColorScheme _colors = Theme.of(context).colorScheme;


  @override
  void initState() {
    initData();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder<bool>(
      future: getData(),
      builder: (BuildContext context, AsyncSnapshot<bool> snapshot) {
        if (!snapshot.hasData) {
          return const SizedBox();
        } else {
          return Container(
            color: AppTheme.background,
            child: ListView.builder(
              // controller: widget.scrollController,
              padding: EdgeInsets.only(
                top: 24,
                bottom: MediaQuery.of(context).padding.bottom,
              ),
              itemCount: listViews.length,
              scrollDirection: Axis.vertical,
              itemBuilder: (BuildContext context, int index) {
                widget.animationController?.forward();
                return listViews[index];
              },
            ),
          );
        }
      },
    );
  }

  // 模拟数据通信
  Future<bool> getData() async {
    await Future<dynamic>.delayed(const Duration(milliseconds: 50));
    return true;
  }

  // test 初始化所有数据
  void initData() {
    const int count = 4;

    listViews.add(
      GreenHouseCurtainController(
        mainScreenAnimation: Tween<double>(begin: 0.0, end: 1.0).animate(
            CurvedAnimation(
                parent: widget.animationController!,
                curve: Interval((1 / count) * 3, 1.0,
                    curve: Curves.fastOutSlowIn))),
        mainScreenAnimationController: widget.animationController!,
      ),
    );
    listViews.add(
      GreenHouseWindController(
        mainScreenAnimation: Tween<double>(begin: 0.0, end: 1.0).animate(
            CurvedAnimation(
                parent: widget.animationController!,
                curve: Interval((1 / count) * 3, 1.0,
                    curve: Curves.fastOutSlowIn))),
        mainScreenAnimationController: widget.animationController!,
      ),
    );
    listViews.add(
      WaterPumpController(
        mainScreenAnimation: Tween<double>(begin: 0.0, end: 1.0).animate(
            CurvedAnimation(
                parent: widget.animationController!,
                curve: Interval((1 / count) * 3, 1.0,
                    curve: Curves.fastOutSlowIn))),
        mainScreenAnimationController: widget.animationController!,
      ),
    );
    listViews.add(
      GlassView(
          animation: Tween<double>(begin: 0.0, end: 1.0).animate(
              CurvedAnimation(
                  parent: widget.animationController!,
                  curve: Interval((1 / count) * 4, 1.0,
                      curve: Curves.fastOutSlowIn))),
          animationController: widget.animationController!),
    );


  }
}
